package luj.tool.jnproxyan.proxy.network.socks.actor.root.message.request.connect;

import com.google.common.collect.ImmutableSet;
import luj.bean.api.BeanContext;
import luj.data.api.DataContext;
import luj.tool.jnproxyan.proxy.network.direct.HttpsDirectConnector;
import luj.tool.jnproxyan.proxy.network.socks.actor.root.Socks5Actor;
import luj.tool.jnproxyan.proxy.network.socks.reply.SocksConnectReplier;
import luj.tool.jnproxyan.proxy.network.socks.request.RequestBody;
import luj.tool.jnproxyan.proxy.network.socks.request.SocksRequest;
import luj.tool.jnproxyan.proxy.stats.history.add.ConnectHistoryAdder;
import luj.tool.jnproxyan.proxy.util.data.DataDirtySaver;
import luj.tool.jnproxyan.proxy.util.data.wrap.ResultGlobalLWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.base.Preconditions.checkNotNull;

/**
 * 接收完浏览器的连接请求，接由我们向实际网站发起连接
 */
@Component
final class OnReqConnect implements Socks5Actor.Handler<ReqConnectMsg> {

  @Override
  public void onHandle(Context ctx) throws Exception {
    Socks5Actor self = ctx.getActorState(this);
    ReqConnectMsg msg = ctx.getMessage(this);

    Map<Long, SocksRequest> reqMap = self.getRequestMap();
    Long reqId = msg.reqId();
    SocksRequest req = reqMap.get(reqId);
    checkNotNull(req, reqId);

    String targetHost = msg.targetHost();
    int targetPort = msg.targetPort();

    RequestBody reqBody = new RequestBody();
    reqBody.setVersion(msg.version());
    reqBody.setMethods(firstNonNull(msg.methods(), ImmutableSet.of()));
    reqBody.setCmd(msg.cmd());
    reqBody.setTargetHost(targetHost);
    reqBody.setTargetPort(targetPort);

    req.setBody(reqBody);
    req.setStatus("#2:准备连接网站");

    // 直连远程网站
    _directConnector.inject(self.getConnectLoop(), targetHost, targetPort, ctx.getActorRef(), reqId).connect();

////    new SshForwardConnector(self.getSshSession(),
////        targetHost, targetPort, ctx.getActorRef(), reqId, _lujbean).connect();
//
//    new TargetServerConnector(self.getConnectionFactory(), targetHost,
//        targetPort, ctx.getActorRef(), reqId, _lujbean).connect();

//    // 增加连接历史
//    DatabaseState dbState = self.getDatabaseState();
//    UseGroup using = _lujdata.createUseGroup();
//    ConnectStats stats = _dataLoader.load(dbState, using).andWrap(ConnectStats.class);
//
//    _historyAdder.inject(stats, dbState, using, reqId, targetHost, targetPort).add();
//    _dirtySaver.inject(dbState, using).save();
  }

//  private static final Logger LOG = LoggerFactory.getLogger(OnReqConnect.class);

  @Autowired
  BeanContext _lujbean;

  @Autowired
  ConnectHistoryAdder _historyAdder;

  @Autowired
  DataContext _lujdata;

  @Autowired
  ResultGlobalLWrapper _dataLoader;

  @Autowired
  DataDirtySaver _dirtySaver;

  @Autowired
  HttpsDirectConnector _directConnector;

  @Autowired
  SocksConnectReplier _connectReplier;
}
